package com.hutou.chuangke.fragmentnet;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bilibili.magicasakura.utils.ThemeUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.hutou.chuangke.Bean.Trecruit;
import com.hutou.chuangke.Bean.UserBean;
import com.hutou.chuangke.MainApplication;
import com.hutou.chuangke.R;
import com.hutou.chuangke.fragment.AttachFragment;
import com.hutou.chuangke.net.HttpUtil;
import com.hutou.chuangke.net.RequestThreadPool;
import com.hutou.chuangke.net.http.User;
import com.hutou.chuangke.uitl.LogUtil;
import com.hutou.chuangke.widget.SwipeRefreshLayout;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tyhdark on 2017/3/18.
 * 活动Fragment
 */
public class ActivityFragment extends AttachFragment {
    private SwipeRefreshLayout swipeRefresh;
    private RecyclerView acti_recyclerviewew;
    private ArrayList<Trecruit> mData = new ArrayList<>();
    private ActivityAdapter adapter;
    private int cruepage = 1;//页数
    private int pagesize = 10;//页长
    private boolean isnomore = false;//没有更多了
    private boolean isPulling = false;
    private boolean isLoadmore = false;
    private boolean isFirst = true;
    private Requeslist requeslist;

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_activity,null);
        return view;
    }

    @Override
    public void onViewCreated(View view,  Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isAdded()){
            if(isVisibleToUser&&isFirst){
                isFirst = false;
                requeslist = new Requeslist();
                requeslist.execute();
            }
        }
    }

    private void init(View view) {
        acti_recyclerviewew = (RecyclerView) view.findViewById(R.id.acti_recyclerviewew);
        acti_recyclerviewew.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new ActivityAdapter();
        acti_recyclerviewew.setAdapter(adapter);
        swipeRefresh = (SwipeRefreshLayout) view.findViewById(R.id.swiperefresh);
        swipeRefresh.setHeaderView(crateHeadview());
        swipeRefresh.setFooterView(crateHeadview());
        swipeRefresh.setOnPullRefreshListener(new SwipeRefreshLayout.OnPullRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefresh.setRefreshing(true);
                mData.clear();
                isPulling = true;
                isnomore = false;
                cruepage = 1;
                requeslist.cancleTask();
                requeslist = new Requeslist();
                requeslist.execute();
            }

            @Override
            public void onPullDistance(int distance) {}

            @Override
            public void onPullEnable(boolean enable) {}
        });

        swipeRefresh.setOnPushLoadMoreListener(new SwipeRefreshLayout.OnPushLoadMoreListener() {
            @Override
            public void onLoadMore() {
                swipeRefresh.setLoadMore(true);
                isLoadmore = true;
                requeslist = new Requeslist();
                requeslist.execute();
            }

            @Override
            public void onPushDistance(int distance) {

            }

            @Override
            public void onPushEnable(boolean enable) {

            }
        });

    }

    /**列表请求*/
    class Requeslist extends AsyncTask<String,Void,Boolean>{

        @Override
        protected Boolean doInBackground(String... params) {
            try{
                JsonObject object = HttpUtil.getResposeJsonObject(User.getTrecruitList(cruepage,pagesize));
                JsonArray array = object.getAsJsonArray("trecruitlist");
                if(array.size() == 0){
                    isnomore = true;
                }
                for(JsonElement element : array){
                    mData.add(MainApplication.gsonInstance().fromJson(element,Trecruit.class));
                }
                String flag = object.get("flag").getAsString();
                if(flag.equals("1")){
                    return true;
                }
                return false;
            }catch(Exception e){
                e.printStackTrace();
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean comlete) {
            if(comlete){
                adapter.setData(mData);
                adapter.notifyDataSetChanged();
                if(isnomore){
                    Toast.makeText(getActivity(),"没有更多了",Toast.LENGTH_SHORT).show();
                }else{
                    cruepage++;
                }
                if(isPulling){
                    swipeRefresh.setRefreshing(false);
                    isPulling = false;
                }
                if(isLoadmore){
                    swipeRefresh.setLoadMore(false);
                    isLoadmore = false;
                }
            }else{

            }

        }

        public void cancleTask(){
            cancel(true);
            RequestThreadPool.finish();
        }
    }

    private void loadData(){
    }

    private View crateHeadview() {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View view = inflater.inflate(R.layout.loading_circle,null);
        return view;
    }

    Handler handler = new Handler();

    class ActivityAdapter extends RecyclerView.Adapter<ActivityAdapter.ViewHolder>{
        private ArrayList<Trecruit> mData = new ArrayList<>();
        @Override
        public ActivityAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            ViewHolder holder = new ViewHolder(LayoutInflater.from(getActivity())
                    .inflate(R.layout.item_activity,null));
            return holder;
        }

        @Override
        public void onBindViewHolder(ActivityAdapter.ViewHolder holder, int position) {
            Trecruit trecruit = mData.get(position);
            holder.acti_title.setText(trecruit.getCaption());
            holder.acti_timetv.setText(trecruit.getCrttm());
            holder.acti_num_tv.setText(trecruit.getApplynum());
            holder.acti_share_tv.setText(trecruit.getFwnum());
            if(trecruit.getRstatus().equals("y")){//有限
                holder.acti_type.setImageDrawable(getResources().getDrawable(R.drawable.dym_acti_ing));
            }else{//过期
                holder.acti_type.setImageDrawable(getResources().getDrawable(R.drawable.dym_acti_ed));
            }
            ImageLoader.getInstance().displayImage(trecruit.getLinkurl(), holder.acti_bgimageView);
        }

        public void setData(ArrayList<Trecruit> mData){
            this.mData = mData;
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder{
            TextView acti_title,acti_timetv,acti_num_tv,acti_share_tv;
            ImageView acti_bgimageView,acti_type;
            public ViewHolder(View view) {
                super(view);
                acti_title = (TextView) view.findViewById(R.id.acti_title);
                acti_timetv = (TextView) view.findViewById(R.id.acti_timetv);
                acti_num_tv = (TextView) view.findViewById(R.id.acti_num_tv);
                acti_share_tv = (TextView) view.findViewById(R.id.acti_share_tv);
                acti_bgimageView = (ImageView) view.findViewById(R.id.acti_bgimageView);
                acti_type = (ImageView) view.findViewById(R.id.acti_type);
            }
        }
    }

}
